﻿using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;
using MvvmFoundation.Silverlight;
using SilverlightDemoApp.Model;

namespace SilverlightDemoApp.ViewModel
{
    /// <summary>
    /// ViewModel implementation which uses the Mvvm Foundation.
    /// </summary>
    public class QuestionViewModel
    {
        private readonly PropertyObserver<Number> _observer;

        /// <summary>
        /// Constructor
        /// </summary>
        public QuestionViewModel()
        {
            // Initialize the Questions, ChangeLog and Number
            Initialize();

            // Add property observers to observable objects, using the MVVM Foundation
            _observer =
                new PropertyObserver<Number>(Number)
                    .RegisterHandler(n => n.Value, n => Log("Value: " + n.Value));
        }

        /// <summary>
        /// Contains an ObservableCollection with <see cref="Question"/> objects.
        /// </summary>
        public ObservableCollection<Question> Questions { get; private set; }

        /// <summary>
        /// Contains an ObservableCollection with string objects, this collection will be 
        /// modified when the <see cref="Number"/> has changed.
        /// </summary>
        public ObservableCollection<string> ChangeLog { get; private set; }

        /// <summary>
        /// Contains a <see cref="Number"/> object.
        /// </summary>
        public Number Number { get; private set; }

        /// <summary>
        /// ICommand implementation for clicking a commandbutton in the view.
        /// </summary>
        public ICommand CheckGrade
        {
            get
            {
                var command = new RelayCommand(obj =>
                                                   {
                                                       bool grade = IsGraduated();
                                                       if (grade)
                                                       {
                                                           MessageBox.Show("You are graduated");
                                                       }
                                                       else
                                                       {
                                                           MessageBox.Show("You are not graduated.");
                                                       }
                                                   });

                return command;
            }
        }

        /// <summary>
        /// Initializes the Questions, ChangeLog and Number
        /// </summary>
        private void Initialize()
        {
            var q = new ObservableCollection<Question>();
            q.Add(new Question {Text = "What is 2 + 2 ?", ActualAnswer = "4"});
            q.Add(new Question {Text = "What is 9 - 2 ?", ActualAnswer = "7"});
            Questions = q;

            var n = new Number();
            n.Value = 4;
            Number = n;

            ChangeLog = new ObservableCollection<string>();
        }

        /// <summary>
        /// Callback from Value property in <see cref="Number"/>.
        /// </summary>
        private void Log(string item)
        {
            ChangeLog.Add(item);
        }

        /// <summary>
        /// Checks if the user is graduated based on the Grade properties in the <see cref="Questions"/> collection.
        /// </summary>
        private bool IsGraduated()
        {
            bool grade = true;
            foreach (Question question in Questions)
            {
                grade &= question.Grade;
            }
            return grade;
        }
    }
}